﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace hkgoldenhighlight
{
	public partial class multiMonSettingsForm : Form
	{
		private bool Changed = false;
		internal string[] DisplayModeArray = new string[0];
		internal string[] MonitorsArray = new string[0];
		internal string[] SizeModeArray = new string[0];
		private object LastSelectedItem = new Object();
		
		public multiMonSettingsForm()
		{
			Array.Resize<string>(ref DisplayModeArray, 2);
			DisplayModeArray[0] = "預設（獨立顯示，全部啟用）";
			DisplayModeArray[1] = "獨立顯示";
			Array.Resize<string>(ref SizeModeArray, 5);
			SizeModeArray[0] = "Normal";
			SizeModeArray[1] = "StrechImage";
			SizeModeArray[2] = "AutoSize";
			SizeModeArray[3] = "CenterImage";
			SizeModeArray[4] = "Zoom";
			InitializeComponent();
		}
		
		private void MultiMonSettingsFormLoad(object sender, EventArgs e)
		{
			#region DisplayModeComboBox
			Regex FirstLetterIsASCII = new Regex("[A-Za-z0-9_]");
			if(FirstLetterIsASCII.IsMatch(SettingsForm.SaveType.PadLeft(1)))
			{
				SettingsForm.SaveType = " " + SettingsForm.SaveType;
			}
			this.SaveToButton.Text = this.SaveToButton.Text.Replace("{SaveType}", SettingsForm.SaveType);
			
			int h; for (h = 0; h<DisplayModeArray.Length; h++) {
				DisplayModeComboBox.Items[h] = DisplayModeArray[h];
			}
			if(InfFile.DoesHaveTheFile)
			{
				DisplayModeComboBox.SelectedIndex = Array.IndexOf(DisplayModeArray, InfFile.SelectedDisplayModeComboBox);
			}
			else
			{
				DisplayModeComboBox.SelectedIndex = 0;
			}
			#endregion
			
			#region SeparateedCheckedListBox
			int j; for (j = 0; j<Screen.AllScreens.Length; j++) {
				Array.Resize<string>(ref MonitorsArray, MonitorsArray.Length + 1);
				MonitorsArray[j] = "顯示器 " + (j+1) + " (" + Convert.ToString(Screen.AllScreens[j].Bounds.Width) + "*" + Convert.ToString(Screen.AllScreens[j].Bounds.Height) + ") 位於 (" + Screen.AllScreens[j].Bounds.X + ", " + Screen.AllScreens[j].Bounds.Y + ")";
				SeparatedCheckedListBox.Items.AddRange(new object[] {MonitorsArray[j]});
				SeparatedCheckedListBox.SetItemChecked(j, true);
			}
			#endregion
		}
		
		private void ControlPanelShortcutButtonClick(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start("control.exe","desk.cpl,,3");
		}
		
		private void CancelMultiMonButtonClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		private void SeparatedCheckedListBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			if(SeparatedCheckedListBox.SelectedIndex != -1)
			{
				SeparatedDescriptionLabel.Text = Encoding.UTF8.GetString(Encoding.Default.GetBytes(Screen.AllScreens[SeparatedCheckedListBox.SelectedIndex].DeviceName));
			}
		}
		
		private void SaveToButtonClick(object sender, EventArgs e)
		{
			try {
				InfFile.SaveToInfFile();
				System.Media.SystemSounds.Asterisk.Play();
			} catch (Exception ex) {
				MessageBox.Show(ex.Message, "Save", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}
		
		private void SeparatedCheckedListBoxItemCheck(object sender, ItemCheckEventArgs e)
		{
			Changed = true;
			
			if (e.CurrentValue != e.NewValue && (e.NewValue == CheckState.Checked || e.NewValue == CheckState.Unchecked)) {
				bool Checked = new Boolean();
				if (e.NewValue == CheckState.Checked) {
					Checked = true;
				}else if (e.NewValue == CheckState.Unchecked) {
					Checked = false;
				}
				
				InfFile.SeparatedEnabledDisplays[e.Index] = Checked;
			}
		}
		
		private void DisplayModeComboBoxSelectedIndexChanged(object sender, EventArgs e)
		{
			Changed = true;
			
			InfFile.SelectedDisplayModeComboBox = DisplayModeComboBox.SelectedItem.ToString();
		}
	}
}
